package tt;

import java.util.*;

public class Third {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        TreeMap<Integer, Integer> map = new TreeMap<>();
        map.put(0, 0);
        for (int i = 0; i < n; i++) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            Set<Map.Entry<Integer, Integer>> set = map.entrySet();

            Set<Map.Entry<Integer, Integer>> set1 = new TreeSet<>(new Comparator<Map.Entry<Integer, Integer>>() {
                @Override
                public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                    return o1.getKey() - o2.getKey();
                }
            });
            set1.addAll(set);
            for (Map.Entry<Integer, Integer> integerIntegerEntry : set1) {
                int cmj = integerIntegerEntry.getKey();
                int point = integerIntegerEntry.getValue() + y;
                if (map.get(cmj + x) == null || point > map.get(cmj + x)) {
                    map.put(cmj + x, point);
                }
                if (map.get(cmj - x) == null || point > map.get(cmj - x)) {
                    map.put(cmj - x, point);
                }
            }
        }
        System.out.println(map.get(0));
    }
}
